package com.laizhenghua.wxstub.service.impl;

import com.laizhenghua.wxstub.dto.WxUserDTO;
import com.laizhenghua.wxstub.entiry.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;


/**
 * @description:
 * @author: laizhenghua
 * @date: 2022/9/8 20:59
 */
@Service("userDetailsService")
public class LoginUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Query query = Query.query(Criteria.where("wxId").is(username));
        WxUserDTO userDTO = mongoTemplate.findOne(query, WxUserDTO.class);
        if (userDTO == null) {
            throw new RuntimeException("微信id错误");
        }
        return new LoginUser(userDTO);
    }
}
